package com.example.yunifang;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.RadioButton;
import android.widget.Toast;

import com.example.yunifang.db.GouWuCheOpenHelper;
import com.example.yunifang.fragment.Fragment1;
import com.example.yunifang.fragment.Fragment2;
import com.example.yunifang.fragment.Fragment3;
import com.example.yunifang.fragment.Fragment4;

public class ZhuActivity extends FragmentActivity implements OnClickListener {

	private RadioButton ra1;
	private long mExitTime;
	private BaseActivity activity;
	private RadioButton ra2;
	private RadioButton ra3;
	private RadioButton ra4;
	public static ViewPager zhu_vp;
	int i = FenLeiActivity.i;
	int y = Fragment2.y;

	// private FragmentManager manager;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_zhu);
		GouWuCheOpenHelper helper = new GouWuCheOpenHelper(this);
		helper.getReadableDatabase();
		init();
		activity = new BaseActivity();
		activity.onCreate(this);
		zhu_vp.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
			@Override
			public int getCount() {
				// TODO Auto-generated method stub
				return 4;
			}

			@Override
			public Fragment getItem(int arg0) {
				Fragment fragment = null;
				switch (arg0) {
				case 0:
					fragment = new Fragment1();
					break;
				case 1:
					fragment = new Fragment2();
					break;
				case 2:
					fragment = new Fragment3();

					break;
				case 3:
					fragment = new Fragment4();
					break;

				default:
					break;
				}
				return fragment;
			}
		});

		if (i != 0 || y != 0) {
			zhu_vp.setCurrentItem(1);
		}

	}

	private void init() {
		zhu_vp = (ViewPager) findViewById(R.id.zhu_vp);
		ra1 = (RadioButton) findViewById(R.id.ra1);
		ra2 = (RadioButton) findViewById(R.id.ra2);
		ra3 = (RadioButton) findViewById(R.id.ra3);
		ra4 = (RadioButton) findViewById(R.id.ra4);
		ra1.setOnClickListener(this);
		ra2.setOnClickListener(this);
		ra3.setOnClickListener(this);
		ra4.setOnClickListener(this);
		if (i != 0 || y != 0) {
			ra2.setChecked(true);
			ra2.setTextColor(Color.RED);
		} else {
			ra1.setChecked(true);
			ra1.setTextColor(Color.RED);
		}

	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// �ڵ��back��ʱ��������ж��Ƿ����
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			if ((System.currentTimeMillis() - mExitTime) > 2000) {
				Toast.makeText(this, "再按一次退出", Toast.LENGTH_SHORT).show();
				mExitTime = System.currentTimeMillis();
			} else {
				activity.onDestroy();
			}
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.zhu, menu);
		return true;
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.ra1:
			ra1.setTextColor(Color.RED);
			ra2.setTextColor(Color.BLACK);
			ra3.setTextColor(Color.BLACK);
			ra4.setTextColor(Color.BLACK);
			zhu_vp.setCurrentItem(0);
			break;
		case R.id.ra2:
			ra1.setTextColor(Color.BLACK);
			ra2.setTextColor(Color.RED);
			ra3.setTextColor(Color.BLACK);
			ra4.setTextColor(Color.BLACK);
			zhu_vp.setCurrentItem(1);
			break;
		case R.id.ra3:
			ra1.setTextColor(Color.BLACK);
			ra2.setTextColor(Color.BLACK);
			ra3.setTextColor(Color.RED);
			ra4.setTextColor(Color.BLACK);
			zhu_vp.setCurrentItem(2);
			break;
		case R.id.ra4:
			ra1.setTextColor(Color.BLACK);
			ra2.setTextColor(Color.BLACK);
			ra3.setTextColor(Color.BLACK);
			ra4.setTextColor(Color.RED);
			zhu_vp.setCurrentItem(3);
			break;

		default:
			break;
		}
	}

}
